//
//  SpeedView.swift
//  SpinCut
//
//  Created by dai simon on 2024/7/6.
//

import UIKit
import AudioToolbox

protocol SpeedViewDelegate: NSObjectProtocol {
    func speedViewValueChanged(_ speed: CGFloat)
}

class SpeedView: UIView {
    lazy var bgImageView : UIImageView = {
        let view = UIImageView()
        view.image = UIImage(named: "bg_speed")
        view.isUserInteractionEnabled = false
        return view
    }()
    
    lazy var fastLabel: UILabel = {
        let view = UILabel()
        view.text = "Fast"
        view.font = UIFont.systemFont(ofSize: 12)
        view.textColor = .white
        view.isUserInteractionEnabled = false
        return view
    }()
    
    lazy var slowLabel: UILabel = {
        let view = UILabel()
        view.text = "Slow"
        view.font = UIFont.systemFont(ofSize: 12)
        view.textColor = UIColor(hex: "#FFCF33")
        view.isUserInteractionEnabled = false
        return view
    }()
    
    lazy var stopLabel: UILabel = {
        let view = UILabel()
        view.text = "Stop"
        view.font = UIFont.systemFont(ofSize: 12)
        view.isUserInteractionEnabled = false
        view.textColor = .white
        return view
    }()
    
    lazy var cursor: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "#FFC300")
        view.isUserInteractionEnabled = false
        return view
    }()
    
    var lastPoint: CGPoint = .zero
    var offsetX: CGFloat = 140 + 11
    
    weak var delegate: SpeedViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(bgImageView)
        bgImageView.snp.makeConstraints { make in
            make.width.equalTo(280)
            make.height.equalTo(10)
            make.top.equalToSuperview().offset(16)
            make.centerX.equalToSuperview()
        }
        
        addSubview(fastLabel)
        fastLabel.snp.makeConstraints { make in
            make.top.equalTo(bgImageView.snp.bottom).offset(9)
            make.left.equalToSuperview()
        }
        
        addSubview(slowLabel)
        slowLabel.snp.makeConstraints { make in
            make.centerY.equalTo(fastLabel)
            make.centerX.equalToSuperview()
        }
        
        addSubview(stopLabel)
        stopLabel.snp.makeConstraints { make in
            make.centerY.equalTo(fastLabel)
            make.right.equalToSuperview()
        }
        
        addSubview(cursor)
        cursor.snp.makeConstraints { make in
            make.width.equalTo(2)
            make.height.equalTo(25)
            make.left.equalToSuperview().offset(offsetX)
            make.top.equalToSuperview()
        }
        
        self.isUserInteractionEnabled = true
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.actionPan(gesture:)))
        self.addGestureRecognizer(panGesture)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func actionPan(gesture: UIPanGestureRecognizer) {
        let point = gesture.location(in: self)
        switch gesture.state {
        case .began:
            lastPoint = point
            break
        case .changed:
            let disX = point.x - lastPoint.x
            offsetX = offsetX + disX
            let offset = max(11, min(offsetX, 290))
            cursor.snp.updateConstraints { make in
                make.left.equalToSuperview().offset(offset)
            }
            lastPoint = point
            
            AudioServicesPlaySystemSound(1519)
            let currentValue = offset - 11
            let totalValue : CGFloat = 280
            delegate?.speedViewValueChanged(1.0 - currentValue / totalValue)
            break
        case .ended:
            break
        default:
            break
        }
    }
}
